/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bayu.graph;

import com.bayu.common.lib.CommonUtil;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import com.bayu.controller.MainController;
import com.bayu.services.dao.PreInspectionDao;
import com.bayu.services.entity.Preinspection;
import java.awt.Color;
import java.io.OutputStream;
import java.util.List;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 *
 * @author Bayu
 */
@Controller
public class InspectionGraph {
    
    private String query;
    PreInspectionDao preInspectionDao;

    @Autowired
    private void setInpectionDao(PreInspectionDao preInspectionDao){
      this.preInspectionDao = preInspectionDao;
    }
    @RequestMapping("/preinsp-graph.png")
    private void preinspGraph (OutputStream stream) throws Exception{
        
        List maxValue= preInspectionDao.custom("select max(daily) from Preinspection where date between '"+MainController.startDate+"' and '"+MainController.endDate+"'");
        List maxValue2 = preInspectionDao.custom("select max(accum) from Preinspection where date between '"+MainController.startDate+"' and '"+MainController.endDate+"'");
        String strMax2 = maxValue2.get(0).toString();
        String strMax = maxValue.get(0).toString();
        System.out.println(strMax2+"-"+strMax);

        double max1 = Double.valueOf(strMax).doubleValue()+1;
        double max2 = Double.valueOf(strMax2).doubleValue()+1;
        


        double min = 0.0;
        //double max = maxtemp+2;
        JFreeChart chart = getPreInspGraph(min, max1, max2);
        ChartUtilities.writeChartAsPNG(stream, chart, 1480,800);
    }
    private JFreeChart getPreInspGraph(Double min,double max1,double max2){
       DefaultCategoryDataset dataset1 = inspDataset1();
       
        String tgl1="";
      String tgl2="";
      String title = "REJECTION PRE INSPECTION PIPE FITTING";

      if(!MainController.startDate.equals("")){
          tgl1 = CommonUtil.formatStringDate(MainController.startDate);
          tgl2 = CommonUtil.formatStringDate(MainController.endDate);
          title = title+"\r\n"+ tgl1+"-"+tgl2;
      }
       final JFreeChart chart = ChartFactory.createLineChart(
            title,        // chart title
           "Date",               // domain axis label
           "(% Daily)",                  // range axis label
            dataset1,                 // data
            PlotOrientation.VERTICAL,
            true,                     // include legend
            true,                     // tooltips?
            false                     // URL generator?  Not required...
        );
        chart.setBackgroundPaint(new Color(204, 255, 204));
        CategoryPlot plot = chart.getCategoryPlot();

        plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
        plot.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);
        
        CategoryItemRenderer renderer1 = plot.getRenderer();
        renderer1.setSeriesPaint(0, new Color(153,51,102));
        renderer1.setBaseNegativeItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.INSIDE12, TextAnchor.TOP_CENTER));
        renderer1.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer1.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
        renderer1.setBaseItemLabelsVisible(true);

        DefaultCategoryDataset dataset2 = inspDataset2();
        ValueAxis axis2 = new NumberAxis("% Accum");
        axis2.setAutoRange(false);
        axis2.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        axis2.setRange(min,max2);
        
       plot.setRangeAxis(1, axis2);

    plot.setDataset(1, dataset2);
    plot.mapDatasetToRangeAxis(1, 1);
    CategoryItemRenderer renderer2 = new LineAndShapeRenderer();
    renderer2.setSeriesPaint(0, Color.BLUE);
    renderer2.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    renderer2.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.INSIDE8, TextAnchor.TOP_CENTER));
    renderer2.setBaseItemLabelsVisible(true);
    plot.setRenderer(1, renderer2);

    plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);

    NumberAxis rangeAxis = (NumberAxis)plot.getRangeAxis();
    rangeAxis.setAutoRange(false);
    rangeAxis.setRange(min,max1);
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

       return chart;
    }


    public DefaultCategoryDataset inspDataset1(){
        String rowValue="Daily";
        DefaultCategoryDataset data1 = new DefaultCategoryDataset();
        List data = this.preInspectionDao.getAllByDate(MainController.startDate, MainController.endDate);
        //List data = this.rejectGalvaDao.getAllByDate("2010-12-01", "2010-12-31");
        for(int i=0;i<data.size();i++){
          Preinspection dt = (Preinspection)data.get(i);
          String cat[] = dt.getDate().toString().split("-");
          data1.addValue(dt.getDaily(), rowValue, cat[2]);

          System.out.println("Daily "+dt.getDaily()+"-"+cat[2]);
        }
              
        return data1;
    }

    public DefaultCategoryDataset inspDataset2(){
        String rowValue="Accum";
        DefaultCategoryDataset data2 = new DefaultCategoryDataset();
        List data = this.preInspectionDao.getAllByDate(MainController.startDate, MainController.endDate);
        //List data = this.rejectGalvaDao.getAllByDate("2010-12-01", "2010-12-31");
        for(int i=0;i<data.size();i++){
          Preinspection dt = (Preinspection)data.get(i);
          String cat[] = dt.getDate().toString().split("-");
          data2.addValue(dt.getAccum(), rowValue, cat[2]);

          System.out.println("Accum "+dt.getAccum()+"-"+cat[2]);
        }

        return data2;
    }
    
            
}
